<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <base href="${pageContext.request.contextPath}/">
    <link href="bootstrap/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
    <%--bootstrap-table核心css--%>
    <link rel="stylesheet" type="text/css" href="bootstrap-table/bootstrap-table.min.css">
    <%--bootstrap-datetimepicker核心css--%>
    <link rel="stylesheet" type="text/css" href="bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css">

</head>
<body>
<!--路径导航-->
<ol class="breadcrumb">
    <li>工作台</li>
    <li class="active">线索(潜在客户)</li>
</ol>
<div class="panel-body">
    <!--条件查询-->
    <div class="well well-sm">
        <form id="searchForm" class="form-inline" style="margin-bottom: 0">
            <div class="row" style="margin-bottom: 15px;">
                <div class="form-group col-md-3">
                    <label>&emsp;&emsp;名称</label>
                    <input type="text" id="fullName" class="form-control">
                </div>
                <div class="form-group col-md-3">
                    <label>公司</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="company"/>
                    </div>
                </div>
                <div class="form-group col-md-3">
                    <label>公司座机</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="phone"/>
                    </div>
                </div>
                <div class="form-group col-md-3">
                    <label>线索来源</label>
                    <div class="input-group">
                        <select type="text" class="form-control sourceSelect" id="source" style="width: 183px";></select>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="form-group col-md-3">
                    <label>&emsp;所有者</label>
                    <input type="text" id="owner" class="form-control"/>
                </div>
                <div class="form-group col-md-3">
                    <label>手机</label>
                    <input id="mphone" class="form-control ownerSelect" style="width: 196px;"/>
                </div>
                <div class="form-group col-md-3">
                    <label>线索状态</label>
                    <select id="state" class="form-control stateSelect" style="width: 196px;">
                    </select>
                </div>
                <div class="form-group col-md-3">
                    <button type="button" class="btn btn-primary" onclick="$('#tab').bootstrapTable('refresh');">
                        <span class="glyphicon glyphicon-search"> 搜索</span>
                    </button>
                    <button type="button" class="btn btn-primary"
                            onclick="$('#searchForm')[0].reset();$('#tab').bootstrapTable('refresh');">
                        <span class="glyphicon glyphicon-refresh"> 清空</span>
                    </button>
                </div>
            </div>
        </form>
    </div>
    <!--工具栏和表格-->
    <div id="toolbar">
        <button type="button" class="btn btn-primary" onclick="$('#saveWin').modal('show');">
            <span class="glyphicon glyphicon-plus"> 新增</span>
        </button>
        <button type="button" class="btn btn-danger" onclick="removeBatch()">
            <span class="glyphicon glyphicon-trash"> 批量删除</span>
        </button>
    </div>
    <table id="tab" class="table table-hover table-striped table-bordered"></table>
</div>
<!--模态窗-->
<div id="saveWin" class="modal fade dataWin">
    <div class="modal-dialog" style="width: 90%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button>
                <h4 class="modal-title">创建线索</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal dataForm">
                    <div class="form-group">
                        <div class="errorMsg" style="text-align: center;color: red;"></div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">所有者<span style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select id="save-owner" class="form-control ownerSelect">
                            </select>
                        </div>
                        <label class="col-sm-2 control-label">公司<span style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="save-company">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">称呼</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select id="save-appellation" class="form-control appellationSelect">
                            </select>
                        </div>
                        <label class="col-sm-2 control-label">姓名<span style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                                <input type="text" class="form-control" id="save-fullName"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">职位</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="save-job">
                        </div>
                        <label class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="save-email">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">公司座机</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="save-phone">
                        </div>
                        <label class="col-sm-2 control-label">公司网站</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="save-website">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">手机</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="save-mphone">
                        </div>
                        <label class="col-sm-2 control-label">线索状态</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select id="save-state" class="form-control stateSelect"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">线索来源</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select id="save-source" class="form-control sourceSelect">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">线索描述</label>
                        <div class="col-sm-10" style="width: 60%;">
                            <textarea class="form-control" rows="3" id="save-description"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">联系纪要</label>
                        <div class="col-sm-10" style="width: 60%;">
                            <textarea class="form-control" rows="3" id="save-contactSummary"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">下次联系时间</label>
                        <div class="col-sm-10" style="width: 300px";>
                            <div class="input-group date myDatetime" >
                                <input pyte="text" class="form-control" id="save-nextContactTime" readonly />
                                <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">详细地址</label>
                        <div class="col-sm-10" style="width: 60%;">
                            <textarea class="form-control" rows="3" id="save-address"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="save()">保存</button>
            </div>
        </div>
    </div>
</div>

<div id="editWin" class="modal fade dataWin">
    <div class="modal-dialog" style="width: 90%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button>
                <h4 class="modal-title">编辑线索</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal dataForm">
                    <input type="hidden" id="edit-id">
                    <div class="form-group">
                        <div class="errorMsg" style="text-align: center;color: red;"></div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">所有者<span style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select id="edit-owner" class="form-control ownerSelect">
                            </select>
                        </div>
                        <label class="col-sm-2 control-label">公司<span style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-company">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">称呼</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select id="edit-appellation" class="form-control appellationSelect">
                            </select>
                        </div>
                        <label class="col-sm-2 control-label">姓名<span style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-fullName"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">职位</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-job">
                        </div>
                        <label class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-email">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">公司座机</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-phone">
                        </div>
                        <label class="col-sm-2 control-label">公司网站</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-website">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">手机</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-mphone">
                        </div>
                        <label class="col-sm-2 control-label">线索状态</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select id="edit-state" class="form-control stateSelect"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">线索来源</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select id="edit-source" class="form-control sourceSelect">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">线索描述</label>
                        <div class="col-sm-10" style="width: 60%;">
                            <textarea class="form-control" rows="3" id="edit-description"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">联系纪要</label>
                        <div class="col-sm-10" style="width: 60%;">
                            <textarea class="form-control" rows="3" id="edit-contactSummary"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">下次联系时间</label>
                        <div class="col-sm-10" style="width: 300px";>
                            <div class="input-group date myDatetime" >
                                <input pyte="text" class="form-control" id="edit-nextContactTime" readonly />
                                <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">详细地址</label>
                        <div class="col-sm-10" style="width: 60%;">
                            <textarea class="form-control" rows="3" id="edit-address"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="edit()">保存</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<%--bootstrap-table核心js--%>
<script type="text/javascript" src="bootstrap-table/bootstrap-table.min.js"></script>
<%--bootstrap-table中文语言包--%>
<script type="text/javascript" src="bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<%--bootstrap-datetimepicker核心js--%>
<script type="text/javascript" src="bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<%--bootstrap-datetimepicker中文语言包--%>
<script type="text/javascript" src="bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
</body>
<script type="text/javascript">
    $(function () {
        initTable();
        initRelationName();
        initRelationApll();
        initRelationState();
        initRelationSource()
        //清空模态框中的表单
        $('#saveWin').on('hidden.bs.modal', function () {
            $('.errorMsg').html('');
            $('.dataForm')[0].reset();
        });

        $('#editWin').on('hidden.bs.modal', function () {
            $('.errorMsg').html('');
            $('.dataForm')[1].reset();
        });

        $('.myDatetime').datetimepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd', //日期格式
            weekStart: 1, //一周从周一开始
            autoclose: true, //选择日期后关闭
            startView: 'month', //默认打开月
            minView: 'month',   //精确到年月日，不带时分秒
            todayBtn: true,
            forceParse: false
        });
    });

    //初始化表格
    function initTable() {
        //调用组件配置异步表格
        $('#tab').bootstrapTable({
            //http请求方式
            method: 'get',
            url: 'clue/page',
            //工具栏
            toolbar: '#toolbar',
            //点击选中
            clickToSelect: true,
            //单选
            singleSelect: false,
            //处理响应数据格式
            responseHandler: function (res) {
                if (res.code == 0) {
                    return {
                        rows: res.data.list,
                        total: res.data.total
                    };
                }
            },
            //开启分页
            pagination: true,
            //设置服务器端分页
            sidePagination: 'server',
            //设置每页条数
            pageSize: 10,
            //设置每页条数列表
            pageList: [10, 20, 50, 100],
            paginationPreText: '上一页',
            paginationNextText: '下一页',
            //设置bootstrapTable向后台提交分页数据
            queryParamsType: '',
            //查询参数
            queryParams: function (params) {
                params.fullName = $('#fullName').val();
                params.company = $('#company').val();
                params.phone = $('#phone').val();
                params.source = $('#source').val();
                params.mphone = $('#mphone').val();
                params.state = $('#state').val();
                return params;
            },
            showRefresh: true,
            showToggle: true,
            showFullscreen: true,
            //表格中列描述
            columns: [
                {
                    checkbox: true
                }, {
                    field: 'id',
                    visible: false
                }, {
                    title: '名称',
                    field: 'fullName',
                    halign: 'center',
                    //列数据格式化
                    formatter: function (value, row) {
                        //格式化之后的列数据
                        return '<a href="clue/detail/' + row.id + '">' + value + '</a>';
                    }
                }, {
                    title: '公司',
                    field: 'company',
                    halign: 'center'
                }, {
                    title: '公司座机',
                    field: 'phone',
                    halign: 'center'
                }, {
                    title: '手机',
                    field: 'mphone',
                    halign: 'center'
                }, {
                    title: '线索来源',
                    field: 'source',
                    halign: 'center'
                }, {
                    title: '所有者',
                    field: 'userName',
                    halign: 'center'
                }, {
                    title: '线索状态',
                    field: 'state',
                    halign: 'center'
                }, {
                    title: '操作',
                    halign: 'center',
                    align: 'center',
                    formatter: function (value, row) {
                        return '<button type="button" title="编辑" class="btn btn-warning btn-xs" onclick="get(\'' + row.id +
                            '\')"><span class="glyphicon glyphicon-edit"></span></button>&emsp;' +
                            '<button type="button" title="删除" class="btn btn-danger btn-xs" onclick="remove(\'' + row.id +
                            '\')"><span class="glyphicon glyphicon-trash"></span></button>';
                    }
                }
            ]
        });
    }

    //加载所有者关联信息，初始化下拉列表
    function initRelationName() {
        $.ajax('user/list', {
            type: 'get',
            success: function (res) {
                if(res.code == 0){
                    var array = res.data;
                    var str = '<option value=""></option>';
                    $.each(array, function () {
                        str += '<option value="' + this.id + '">' + this.name + '</option>';
                    });
                    $('.ownerSelect').html(str);
                }
            }
        })
    }

    //加载称呼关联信息，初始化下拉列表
    function initRelationApll() {
        $.ajax('dictionary/value/list', {
            type: 'get',
            data:{
                typeCode:"appellation"
            },
            success: function (res) {
                if(res.code == 0){
                    var array = res.data;
                    var str = '<option value=""></option>';
                    $.each(array, function () {
                        str += '<option value="' + this.id + '">' + this.text + '</option>';
                    });
                    $('.appellationSelect').html(str);
                }
            }
        })
    }

    //加载线索状态关联信息，初始化下拉列表
    function initRelationState() {
        $.ajax('dictionary/value/list', {
            type: 'get',
            data:{
                typeCode:"state"
            },
            success: function (res) {
                if(res.code == 0){
                    var array = res.data;
                    var str = '<option value=""></option>';
                    $.each(array, function () {
                        str += '<option value="' + this.id + '">' + this.text + '</option>';
                    });
                    $('.stateSelect').html(str);
                }
            }
        })
    }

    //加载线索来源关联信息，初始化下拉列表
    function initRelationSource() {
        $.ajax('dictionary/value/list', {
            type: 'get',
            data:{
                typeCode:"source"
            },
            success: function (res) {
                if(res.code == 0){
                    var array = res.data;
                    var str = '<option value=""></option>';
                    $.each(array, function () {
                        str += '<option value="' + this.id + '">' + this.text + '</option>';
                    });
                    $('.sourceSelect').html(str);
                }
            }
        })
    }

    //新增
    function save() {
        $.ajax('clue/add', {
            type: 'post',
            contentType: 'application/json',
            data: JSON.stringify({
                owner: $('#save-owner').val(),
                company: $('#save-company').val(),
                appellation: $('#save-appellation').val(),
                fullName: $('#save-fullName').val(),
                job: $('#save-job').val(),
                email: $('#save-email').val(),
                phone: $('#save-phone').val(),
                website: $('#save-website').val(),
                mphone: $('#save-mphone').val(),
                state: $('#save-state').val(),
                source: $('#save-source').val(),
                description: $('#save-description').val(),
                contactSummary: $('#save-contactSummary').val(),
                nextContactTime: $('#save-nextContactTime').val(),
                address: $('#save-address').val()
            }),
            beforeSend: function () {
                //前端数据校验
                var owner = $('#save-owner').val();
                if (!owner) {
                    $('.errorMsg').html('请选择所有者');
                    return false;
                }
                var company = $('#save-company').val();
                if (!company) {
                    $('.errorMsg').html('公司名称不能为空');
                    return false;
                }
                var fullName = $('#save-fullName').val();
                if (!company) {
                    $('.errorMsg').html('姓名不能为空');
                    return false;
                }
            },
            success: function (res) {
                if (res.code == 0) {
                    alert('新增成功');
                    //刷新表格
                    $('#tab').bootstrapTable('refresh');
                    //关闭窗体
                    $('#saveWin').modal('hide');
                }else{
                    $('.errorMsg').html(res.message);
                }
            }
        });
    }

    //打开修改
    function get(id) {
        $.ajax('clue/find/' + id, {
            type: 'get',
            success: function (res) {
                if (res.code == 0) {
                    $('#edit-id').val(res.data.id);
                    $('#edit-owner').val(res.data.owner);
                    $('#edit-company').val(res.data.company);
                    $('#edit-appellation').val(res.data.appellation),
                    $('#edit-fullName').val(res.data.fullName),
                    $('#edit-job').val(res.data.job),
                    $('#edit-email').val(res.data.email),
                    $('#edit-phone').val(res.data.phone),
                    $('#edit-website').val(res.data.website),
                    $('#edit-mphone').val(res.data.mphone),
                    $('#edit-state').val(res.data.state),
                    $('#edit-source').val(res.data.source),
                    $('#edit-description').val(res.data.description),
                    $('#edit-contactSummary').val(res.data.contactSummary),
                    $('#edit-nextContactTime').val(res.data.nextContactTime),
                    $('#edit-address').val(res.data.address)
                    $('#editWin').modal('show');
                }else if(res.code = -100){
                    // location.href = 'page/login';
                    parent.location.href = 'page/login';
                }else{
                    alert(res.message);
                }
            }
        });
    }

    //修改
    function edit() {
        $.ajax('clue/edit', {
            type: 'put',
            contentType: 'application/json',
            data: JSON.stringify({
                id : $('#edit-id').val(),
                owner : $('#edit-owner').val(),
                company : $('#edit-company').val(),
                appellation : $('#edit-appellation').val(),
                fullName : $('#edit-fullName').val(),
                job : $('#edit-job').val(),
                email : $('#edit-email').val(),
                phone : $('#edit-phone').val(),
                website : $('#edit-website').val(),
                mphone : $('#edit-mphone').val(),
                state : $('#edit-state').val(),
                source : $('#edit-source').val(),
                description : $('#edit-description').val(),
                contactSummary : $('#edit-contactSummary').val(),
                nextContactTime : $('#edit-nextContactTime').val(),
                address : $('#edit-address').val()
            }),
            beforeSend: function () {
                //前端数据校验
            },
            success: function (res) {
                if (res.code == 0) {
                    alert('编辑成功');
                    //刷新表格
                    $('#tab').bootstrapTable('refresh');
                    //关闭窗体
                    $('#editWin').modal('hide');
                }else{
                    $('.errorMsg').html(res.message);
                }
            }
        });
    }

    //删除
    function remove(id) {
        if (confirm('确定删除数据吗？')) {
            $.ajax('clue/deleteById/' + id, {
                type: 'delete',
                success: function (res) {
                    if (res.code == 0) {
                        alert('删除成功');
                        $('#tab').bootstrapTable('refresh');
                    }else{
                        alert(res.message);
                    }
                }
            });
        }
    }

    //批量删除
    function removeBatch() {
        //获取用户在表格中选择
        var row = $('#tab').bootstrapTable('getSelections');
        if (row.length == 0) {
            alert('请选择需要删除的数据.');
        } else {
            if (!confirm('确定删除数据吗?')) {
                return;
            }
            var ids = [];
            $.each(row, function () {
                ids.push(this.id);
            });
            $.ajax('clue/removes', {
                type: 'post',
                data: {
                    _method: 'delete',
                    ids: ids.join()
                },
                success: function (res) {
                    if (res.code == 0) {
                        alert('删除成功');
                        $('#tab').bootstrapTable('refresh');
                    }else{
                        alert(res.message);
                    }
                }
            })
        }
    }
</script>
</html>